/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2021并保留所有权利。
 * Copyright 2021, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.threapool.executor;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class TestCountDownLatch2 {

    private static AtomicInteger id = new AtomicInteger();

    private static ExecutorService executorService = new ThreadPoolExecutor(4, 10, 0,
            TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), new ThreadPoolExecutor.DiscardPolicy());

    public static void main(String[] args) throws Exception {

        CountDownLatch countDownLatch = new CountDownLatch(4);
        for (int i = 0; i < 4; i++) {
            int result = i + 5;
            System.out.println("外面打印result："+result);  
            executorService.submit(() -> {
                        System.out.println("里面打印result："+result);   //这一句用来看一下多线程效果
                        System.out.println(Thread.currentThread());  //刚好4个线程，每个线程执行一个任务
                        System.out.println("麻神" + id.getAndIncrement() + "来了");
                        countDownLatch.countDown();
                    }
            );
        }
        System.out.println("等待所有麻神的到来！");
        countDownLatch.await();
        //countDownLatch.await(3000, TimeUnit.MICROSECONDS);    //避免主线程无法执行到可以用这句
        executorService.shutdown();

        System.out.println("人都到齐了，血战到底......");

    }
}
